Passed
Push — master ( 33081a...0dd98d )
by Paul
05:28
created

Pinned.request_   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
/** global: GLSR, jQuery */
2
;(function( x ) {
3
4
	'use strict';
5
6
	var Pinned = function() {
7
		this.el = x( '#pinned-status-select' );
8
		this.target = null;
9
		if( !this.el )return;
10
		this.init_();
11
	};
12
13
	Pinned.prototype = {
14
		/** @return void */
15
		init_: function() {
16
			x( 'a.cancel-pinned-status' ).on( 'click', this.onClickCancel_.bind( this ));
17
			x( 'a.edit-pinned-status' ).on( 'click', this.onClickEdit_.bind( this ));
18
			x( 'a.save-pinned-status' ).on( 'click', this.onClickSave_.bind( this ));
19
			x( 'table td.pinned i' ).on( 'click', this.onClickToggle_.bind( this ));
20
		},
21
22
		/** @return void */
23
		onClickCancel_: function( ev ) { // MouseEvent
24
			ev.preventDefault();
25
			this.el.slideUp( 'fast' ).siblings( 'a.edit-pinned-status' ).show().focus();
26
			this.el.find( 'select' ).val( x( '#hidden-pinned-status' ).val() === '0' ? 1 : 0 );
27
		},
28
29
		/** @return void */
30
		onClickEdit_: function( ev ) { // MouseEvent
31
			ev.preventDefault();
32
			if( !this.el.is( ':hidden' ))return;
33
			this.el.slideDown( 'fast', function() {
34
				this.el.find( 'select' ).focus();
35
			}.bind( this ));
36
			x( this.el ).hide();
37
		},
38
39
		/** @return void */
40
		onClickSave_: function( ev ) { // MouseEvent
41
			ev.preventDefault();
42
			this.el.slideUp( 'fast' ).siblings( 'a.edit-pinned-status' ).show().focus();
43
			this.target = ev.target;
44
			var request = {
45
				action: 'toggle-pinned',
46
				id: x( '#post_ID' ).val(),
47
				nonce: GLSR.pinned_nonce,
48
				pinned: x( '#pinned-status' ).val(),
49
			};
50
			this.request_( request, this.save_.bind( this ));
51
		},
52
53
		/** @return void */
54
		onClickToggle_: function( ev ) { // MouseEvent
55
			ev.preventDefault();
56
			this.target = ev.target;
57
			var request = {
58
				action: 'toggle-pinned',
59
				id: ev.target.getAttribute( 'data-id' ),
60
				nonce: GLSR.pinned_nonce,
61
			};
62
			this.request_( request, this.togglePinned_.bind( this ));
63
		},
64
65
		/** @return void */
66
		request_: function( request, callback ) { // object, function
67
			var data = {
68
				action: GLSR.action,
69
				request: request,
70
			};
71
			x.post( GLSR.ajaxurl, data, callback.bind( this ));
72
		},
73
74
		/** @return void */
75
		save_: function( response ) {
76
			x( '#pinned-status' ).val( !response.pinned|0 );
77
			x( '#hidden-pinned-status' ).val( response.pinned|0 );
78
			x( '#pinned-status-text' ).text( response.pinned ? this.target.dataset.yes : this.target.dataset.no );
79
			GLSR.Notices( response.notices );
80
		},
81
82
		/** @return void */
83
		togglePinned_: function( response ) {
84
			this.target.classList[response.pinned ? 'add' : 'remove']( 'pinned' );
85
		},
86
	};
87
88
	GLSR.Pinned = Pinned;
89
})( jQuery );
90